package it.xiuxian.personcenter.activity

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType

import com.tbruyelle.rxpermissions2.RxPermissions
import com.zhihu.matisse.Matisse
import it.xiuxian.lib.base.App
import it.xiuxian.lib.base.AppManager
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.utils.*
import it.xiuxian.personcenter.R
import it.xiuxian.personcenter.bean.ImageBean
import it.xiuxian.personcenter.databinding.ActivityFaceBinding
import it.xiuxian.personcenter.presenter.FaceActivityPresenter
import it.xiuxian.personcenter.util.GlideEngine
import kotlinx.android.synthetic.main.activity_face.*
import java.io.File
import java.util.*

@Route(path = ArouterAddress.FACEACTIVITY)
class FaceActivity:BaseActivity<FaceActivityPresenter>() {
   /* @JvmField
    @Autowired*/
    var idcard:String?=null
    @JvmField
    @Autowired
    var name:String?=null
   lateinit var binding:ActivityFaceBinding
    var picMap = HashMap<ImageView, String>()
    lateinit private var ivSelect:ImageView//当前选中的
    override val layoutId: Int
        get() = R.layout.activity_face
    override val presenter: FaceActivityPresenter
        get() = FaceActivityPresenter()

    override fun initView() {
        ARouter.getInstance().inject(this)
       binding =  getViewDataBinding<ActivityFaceBinding>()
        (binding.include.re.layoutParams as LinearLayout.LayoutParams).setMargins(0,
            DensityUtil.getStatusPix(App.INSTANCE.applicationContext)+5,0,0)

        binding.include.tvTitle.setTextColor(resources.getColor(R.color.lib_view))
        binding.include.leftImg.setColorFilter(resources.getColor(R.color.lib_view))

    }

    override fun initEvent(){
        //super.initEvent()
        binding.ivCardFront.setOnClickListener {
            ivSelect = binding.ivCardFront
            selectPic()
        }
        binding.ivCardBack.setOnClickListener {
            ivSelect = binding.ivCardBack
            selectPic()
        }
        binding.ivCardHolder.setOnClickListener {
            ivSelect = binding.ivCardHolder
            selectPic()
        }
        binding.tvSure.setOnClickListener {
            if(picMap.size==1){
                mPresenter!!.shiming("",name!!,"","",picMap[ivCardHolder]!!)
            }else{
                ToastUtil.showShort("请您图片填全后再提交!")
            }


        }
    }
    @SuppressLint("CheckResult")
   fun selectPic(){
        RxPermissions(this)
            .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
            .subscribe(io.reactivex.functions.Consumer<Boolean> { granted: Boolean ->
                if (granted) {
                    PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .previewImage(true)
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .isCamera(true)
                        .imageFormat(PictureMimeType.PNG_Q)
                        .maxSelectNum(1)
                        .compress(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST)
                }
            })

    }
    fun setData(msg: String) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
        LiveDataBus.get().with(RxBusCode.FACE).postValue(1357)
        AppManager.getSingleton().finishAllActivity("ShiMingActivity")
        finish()
    }
     var filePath: String? = null
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (resultCode == RESULT_OK){
                when (requestCode){
                    PictureConfig.CHOOSE_REQUEST -> {
                        val selectList = PictureSelector.obtainMultipleResult(data)
                        val media = selectList[0]
                        if (media.isCompressed) {
                            filePath = media.compressPath
                        } else {
                            filePath = media.path
                        }
                        mPresenter!!.onload(File(filePath))
                    }
                }
            }

        }

    fun updata(data: ImageBean){
        Glide.with(this).applyDefaultRequestOptions(
            RequestOptions().diskCacheStrategy(
            DiskCacheStrategy.NONE).skipMemoryCache(true)).load(data.imgurl).into(ivSelect)
            picMap[ivSelect] = data.imgurl
    }


}